/**
 * SpeedBoostPowerUp - 加速道具
 * 临时提高玩家的移动速度
 */

import { PowerUp, PowerUpType, PowerUpRarity, type PowerUpConfig, type PowerUpEffect, type PowerUpOptions } from '../PowerUp'
import type { Player } from '../Player'

/**
 * 加速效果实现
 */
export class SpeedBoostEffect implements PowerUpEffect {
  private originalRunSpeed: number = 0
  private originalSlideSpeed: number = 0
  private speedMultiplier: number = 1.5 // 速度倍数

  /**
   * 应用加速效果
   */
  apply(player: Player): void {
    // 保存原始速度
    this.originalRunSpeed = (player as any).runSpeed || 200
    this.originalSlideSpeed = (player as any).slideSpeed || 150

    // 应用速度加成
    if ((player as any).runSpeed !== undefined) {
      (player as any).runSpeed *= this.speedMultiplier
    }
    if ((player as any).slideSpeed !== undefined) {
      (player as any).slideSpeed *= this.speedMultiplier
    }

    console.log(`加速效果已激活，速度提升 ${(this.speedMultiplier - 1) * 100}%`)
  }

  /**
   * 移除加速效果
   */
  remove(player: Player): void {
    // 恢复原始速度
    if ((player as any).runSpeed !== undefined) {
      (player as any).runSpeed = this.originalRunSpeed
    }
    if ((player as any).slideSpeed !== undefined) {
      (player as any).slideSpeed = this.originalSlideSpeed
    }

    console.log('加速效果已结束')
  }

  /**
   * 更新加速效果
   */
  update(_player: Player, _deltaTime: number): void {
    // 加速效果通常不需要每帧更新
    // 可以在这里添加粒子效果或其他视觉反馈
  }

  /**
   * 获取速度倍数
   */
  getSpeedMultiplier(): number {
    return this.speedMultiplier
  }
}

/**
 * 加速道具类
 */
export class SpeedBoostPowerUp extends PowerUp {
  constructor(options: Omit<PowerUpOptions, 'powerUpType'>) {
    super({
      ...options,
      powerUpType: PowerUpType.SPEED_BOOST
    })
  }

  /**
   * 创建加速道具配置
   */
  protected createConfig(customConfig?: Partial<PowerUpConfig>): PowerUpConfig {
    const defaultConfig: PowerUpConfig = {
      type: PowerUpType.SPEED_BOOST,
      duration: 6000, // 6秒
      value: 1.5, // 速度倍数
      rarity: PowerUpRarity.COMMON,
      stackable: true,
      maxStacks: 3,
      description: '临时提高移动速度50%',
      iconColor: '#2196F3' // 蓝色
    }

    return { ...defaultConfig, ...customConfig }
  }

  /**
   * 创建加速效果
   */
  protected createDefaultEffect(): PowerUpEffect {
    return new SpeedBoostEffect()
  }

  /**
   * 渲染加速道具的特殊效果
   */
  protected renderDefault(ctx: CanvasRenderingContext2D): void {
    // 绘制闪电形状表示速度
    ctx.fillStyle = this.config.iconColor
    
    const centerX = 0
    const centerY = 0
    const size = Math.min(this.width, this.height) * 0.7
    
    // 绘制闪电形状
    ctx.beginPath()
    ctx.moveTo(centerX - size/4, centerY - size/2)
    ctx.lineTo(centerX + size/6, centerY - size/6)
    ctx.lineTo(centerX - size/8, centerY - size/6)
    ctx.lineTo(centerX + size/4, centerY + size/2)
    ctx.lineTo(centerX - size/6, centerY + size/6)
    ctx.lineTo(centerX + size/8, centerY + size/6)
    ctx.closePath()
    ctx.fill()

    // 绘制速度线条效果
    ctx.strokeStyle = '#64B5F6'
    ctx.lineWidth = 2
    
    for (let i = 0; i < 3; i++) {
      const y = centerY - size/3 + (i * size/3)
      ctx.beginPath()
      ctx.moveTo(centerX + size/2, y)
      ctx.lineTo(centerX + size/2 + 8, y)
      ctx.stroke()
    }
  }
}